package inc.gome.dynamic.datasource.oss;

import com.amazonaws.*;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;

import java.io.IOException;
import java.net.URL;

public class GeneratePresignedURL {



    public static void main(String[] args) throws IOException {
 // 替换为当前集群的信息
       // 替换为当前集群的信息
       String akey = "4SPIVUVBZ5E3UHM3J05S";
       String skey = "204cvxspRKLMdPeFcKsAF5Px8Wnn5tfVYeMcHoKR ";
       String endpoint = "http://bjgc11.gos.gomedc.com/";

       // 替换为指定的桶名和⽂件名
       String bucketName = "demoapp";
       String stringObjKeyName = "demo1";
       String fileObjKeyName = "file1";
;

       // 替换为指定的桶名和⽂件名

       String objectKey = "file1";
       try {
       AWSCredentials credentials = new BasicAWSCredentials(akey,
      skey);
       ClientConfiguration clientConfig = new ClientConfiguration();
       clientConfig.setProtocol(Protocol.HTTP);
       AmazonS3 s3Client = new AmazonS3Client(credentials,
      clientConfig);
       s3Client.setEndpoint(endpoint);

       // 设置预签名 URL 在⼀⼩时后过期
       java.util.Date expiration = new java.util.Date();
       long expTimeMillis = expiration.getTime();
       expTimeMillis += 1000 * 60 * 60;
       expiration.setTime(expTimeMillis);
       // ⽣成预签名 URL
       System.out.println("Generating pre-signed URL.");
       GeneratePresignedUrlRequest generatePresignedUrlRequest =
       new GeneratePresignedUrlRequest(bucketName, objectKey)
       .withMethod(HttpMethod.GET)
       .withExpiration(expiration);
       URL url =
      s3Client.generatePresignedUrl(generatePresignedUrlRequest);


       System.out.println("Pre-Signed URL: " + url.toString());
       }
       catch(AmazonServiceException e) {
       // 服务端错误
       e.printStackTrace();
       }
       catch(AmazonClientException e) {
       // 客户端错误
       e.printStackTrace();
       }
 }
}